﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MuXue.Zyiz.Template.Domain.Model;
using MuXue.Zyiz.Template.Domain.Model.Request;
using MuXue.Zyiz.Template.Service;
using Microsoft.Extensions.Logging;
using MuXue.Zyiz.Template.Common;
using MuXue.Zyiz.Template.Domain.Entity;
using MuXue.Zyiz.Template.Domain.Model.response;
using Microsoft.AspNetCore.Authorization;
using MuXue.Zyiz.Template.Common.Redis;
 
using Newtonsoft.Json;
using MuXue.Zyiz.Template.Common.RabbitMQ;
using Microsoft.Extensions.Configuration;
using MuXue.Zyiz.Template.OuterClient;

namespace MuXue.Zyiz.Template.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
     
    [ApiController]
    public class HealthController : ControllerBase
    {
        private readonly ILogger _logger;
     
       
        private IordersService _ordersService;
        private readonly IConfiguration _config;
        private IMsgApiClient _IMsgApiClient;

        public HealthController(ILogger<HealthController> logger,   IordersService ordersService, IConfiguration config, IMsgApiClient iMsgApiClient)
        {
            _logger = logger;
            _config = config; 
            _ordersService = ordersService;
            _IMsgApiClient = iMsgApiClient;

        }

        /// <summary>
        /// 是否能连接通
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<string>> POk()
        { 
            Result<string> result = new Result<string>();
            //result= await _IMsgApiClient.HealthCheck();
            result.state = true;
            result.data = "连接成功";

            return result;
        }

        /// <summary>
        /// 到redis层激活
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<string>> RedisOk()
        {
            Result<string> result = new Result<string>();

            try
            {
                string redisKey = "health_zyiznet_web";
                string kv = "isok";
                RedisHelper.Set(redisKey, kv, RedisCacheTime.Minute1);

                string retKV = RedisHelper.Get(redisKey);
                if (retKV == kv)
                {
                    result.state = true;
                    result.error_msg = "ok";
                    result.error_code = "";

                    return result;
                }
                else
                {
                    result.state = false;
                    result.error_code = "10001";
                    result.error_msg = "取值不等";
                }
            }
            catch (Exception ex)
            {
                result.state = false;
                result.error_code = "10001";
                result.error_msg = "报错了：" + ex.Message;
                throw;
            }


            return result;
        }


        

    }
}
